package com.netdisk.controller;

import com.netdisk.dto.request.file.DeleteDTO;
import com.netdisk.dto.request.file.RenameDTO;
import com.netdisk.dto.request.file.UploadDTO;
import com.netdisk.dto.response.file.FileDTO;
import com.netdisk.dto.response.file.FileListDTO;
import com.netdisk.mapper.FileMapper;
import com.netdisk.service.FileService;
import com.netdisk.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Api(tags = "文件管理")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;
    @Autowired
    private FileMapper fileMapper;
    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public Result<FileDTO> uploadFile(UploadDTO uploadDTO) throws IOException {
        FileDTO fileDTO = fileService.uploadFile(uploadDTO);
        return Result.success(fileDTO);
    }

    @ApiOperation(value = "获取文件列表")
    @GetMapping("/list")
    public Result<FileListDTO> getFileList(@RequestParam Integer userId){
        FileListDTO fileListDTO =  new FileListDTO();
        Integer fileCount = fileService.getFileCountById(userId);
        List<FileDTO> fileList = fileService.getFileListById(userId);
        fileListDTO.setTotal(fileCount);
        fileListDTO.setList(fileList);
        return Result.success(fileListDTO);
    }

    @ApiOperation(value = "删除文件")
    @DeleteMapping("/delete")
    public Result<Object> deleteFile(DeleteDTO deleteDTO){
        fileService.deleteFile(deleteDTO);
        return Result.success();
    }

    @ApiOperation(value = "重命名文件")
    @PutMapping("/rename")
    public Result<FileDTO> renameFile(RenameDTO renameDTO){
        FileDTO fileDTO = fileService.renameFile(renameDTO);
        return Result.success(fileDTO);
    }

    @ApiOperation(value = "下载文件")
    @GetMapping("/download")
    public ResponseEntity<Resource> downloadFile(@RequestParam Integer userId, @RequestParam Integer fileId) throws Exception{
        return fileService.downloadFile(userId,fileId);
    }
}
